/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import config.CFIBShop.Price;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import org.apache.commons.lang.time.DateUtils;
import share.PGHelper;
import share.TimeUtil;

/**
 *
 * @author Salm
 */
public class CFSaleoff implements JSONable {
//    private final NavigableMap<Long, Sale> sales = new TreeMap();
    private final Sale sale = new Sale();
    
    private CFSaleoff()
    {
        
    }
    
    public static CFSaleoff parse(Map<String, Object> json)
    {
        CFSaleoff saleoff = new CFSaleoff();
        saleoff.deser(json);
        return saleoff;
    }

    @Override
    public void deser(Map<String, Object> json) {
//        for (Map.Entry<String, Object> saleEntry : json.entrySet()) {
//            Map<String, Object> saleJSON = (Map) saleEntry.getValue();
//            Sale sale = new Sale();
//            sale.deser(saleJSON);
//            
//            sales.put(sale.start, sale);
//        }
        
        this.sale.deser(json);
    }
    
    public Sale getSale(long now)
    {
//        Long floored = sales.floorKey(now);
//        if (floored != null)
//        {
//            Sale sale = sales.get(floored);
//            if (sale != null && sale.end > now)
//            {
//                return sale;
//            }
//        }
//        
//        return null;
        
        if (sale.getStart() <= now && sale.getEnd() > now)
        {
            return sale;
        }
        
        return null;
    }
    
    public static class Sale implements JSONable
    {
        private long start, end;
        private final IBShop ibshop = new IBShop();
        private final RPItems rpItems = new RPItems();

        @Override
        public void deser(Map<String, Object> json) {
            String startDay = (String) json.get("startDay");
            String endDay = (String) json.get("endDay");
            
            start = TimeUtil.getDate(startDay).getTime();
            end = DateUtils.addDays(TimeUtil.getDate(endDay), 1).getTime();
            
            Map<String, Object> ibshopJSON = (Map) json.get("ibshop");
            this.ibshop.deser(ibshopJSON);
            Map<String, Object> rpItemsJSON = (Map) json.get("randomize_prize");
            this.rpItems.deser(rpItemsJSON);
        }

        public long getStart() {
            return start;
        }

        public long getEnd() {
            return end;
        }

        public IBShop getIbshop() {
            return ibshop;
        }

        public RPItems getRpItems() {
            return rpItems;
        }
        
        public static class IBShop extends JSONMapString<Price>
        {
            @Override
            protected Price newElement(Map<String, Object> elemJSON) {
                return new Price();
            }

            @Override
            public void deser(Map<String, Object> json) {
                super.deser((Map) json.get("items"));
            }
        }
        
        public static class RPItems extends HashMap<String, Integer> implements JSONable
        {
            @Override
            public void deser(Map<String, Object> json) {
                json = (Map) json.get("items");
                for (Map.Entry<String, Object> entrySet : json.entrySet()) {
                    String itemID = (String) entrySet.getKey();
                    int pz = PGHelper.toInt(entrySet.getValue());
                    
                    this.put(itemID, pz);
                }
            }
        }
    }
}
